package com.study.easyexcel.read;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.util.StringUtils;
import com.study.easyexcel.entity.DetailData;
import com.study.easyexcel.util.FileUtil;

import java.io.InputStream;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 读取excel 工具类
 */
public class ReadUtil {
    private static final String FILE_NAME="0075T231021-200739-101029[001].xlsx";
    private static final String SHEET_NAME="明细数据(029)";

    //测试读取 明细数据
    public static void main(String[] args) throws Exception {
        List<DetailData> data = readData(FILE_NAME, SHEET_NAME);
        //data.forEach(System.out::println);


        Map<String, List<DetailData>> map = data.stream().filter(s->
             !"搁置".equals(s.getMode())
        ).collect(Collectors.groupingBy(DetailData::getMode));
        map.forEach((k,v)->{
            System.out.println(k+" "+v.size());
        });
    }

    /**
     * 读取明细数据
     */
    public static List<DetailData> readData(InputStream inputStream,String sheetName){
        if(inputStream==null){
            throw new RuntimeException("参数inputStream不能为空");
        }
        if(StringUtils.isBlank(sheetName)){
            sheetName=SHEET_NAME;
        }
        List<DetailData> data = EasyExcel.read(inputStream).sheet(sheetName).head(DetailData.class).doReadSync();
        return data;
    }

    /**
     * 读取明细数据
     * @return
     */
    public static List<DetailData> readData(String fileName ,String sheetName) throws Exception {
        if(StringUtils.isBlank(fileName)){
            fileName=FILE_NAME;
        }
        InputStream in = FileUtil.getFileInputStream(fileName);
        return readData(in,sheetName);
    }

}
